package com.loongtech.bi.action.flow;

import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

@Controller
@Scope("view")
public class AuanyTransferorderFlowAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
	protected DecimalFormat df = new DecimalFormat("######0.00");
	protected Date dateStart = new Date(Time.getBeginningOfDay(Time
			.currentTime()));
	protected Date dateEnd = new Date(
			Time.getBeginningOfDay(Time.currentTime()));

	private AuanyTransferorderFlow queryBean; // 查询bean

	private List<Object[]> result; // 结果List

	@Autowired
	HadoopQueryManager hadoopQueryManager;

	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;

	@PostConstruct
	public void init() {
		queryBean = new AuanyTransferorderFlow();
		result = new ArrayList<>();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "流水查询—支付订单流水");

		result = new ArrayList<>();

		FacesContext context = FacesContext.getCurrentInstance();

		// 开始结束时间不能大于当前时间60天
		if (Time.getInterval(new Date(), dateStart) > 60
				|| Time.getInterval(new Date(), dateEnd) > 60) {
			context.addMessage(null, new FacesMessage("提示",
					"开始结束时间不能小于当前时间60天!"));
			return;
		}

		// 全用户名，角色ID必须至少有一个非空!
		if (StringUtils.isBlank(queryBean.getFulluseridentity())
				&& StringUtils.isBlank(queryBean.getRoleid())) {
			context.addMessage(null, new FacesMessage("提示",
					"全用户名，角色ID必须至少有一个非空!"));
			return;
		}

		if (HadoopQueryManager.VISIT_QUEUE.offer("AuanyTransferorderFlow")) {
			System.out.println("VISIT_QUEUE offer AuanyTransferorderFlow ok!");

			StringBuffer sql = new StringBuffer();
			sql.append("select logtime,hostname,modulename,gameid,auanyserverid,logid,logtag,ordertype,fulluseridentity,serverid,roleid,gameorderid,createtime,gameproductid,ordercurrencytype,gameproductamount,platname,platorderid,platproductid,platcurrencytype,platproductamount,gamecurrencytype,gamecurrencynum,extracurrencynum,retcode,synsn,acksn,extra1,extra2 from auany_transferorder where 1=1 ");
            if (!StringUtils.isBlank(queryBean.getRoleid())) {
                sql.append(" and roleid = '");
                sql.append(queryBean.getRoleid());
                sql.append("' ");
            }
			if (!"0".equals(queryServer)) {
				sql.append(" and serverid = '");
				sql.append(queryServer);
				sql.append("' ");
			}
			if (queryBean.getAuanyserverid() != null
					&& !"".equals(queryBean.getAuanyserverid())) {
				sql.append(" and auanyserverid = '");
				sql.append(queryBean.getAuanyserverid().trim());
				sql.append("' ");
			}
			sql.append(" and ordertype = ");
			sql.append(queryBean.getOrdertype());
			if (queryBean.getFulluseridentity() != null
					&& !"".equals(queryBean.getFulluseridentity())) {
				sql.append(" and fulluseridentity = '");
				sql.append(queryBean.getFulluseridentity().trim());
				sql.append("' ");
			}

			if (queryBean.getGameproductid() != null
					&& !"".equals(queryBean.getGameproductid())) {
				sql.append(" and gameproductid = '");
				sql.append(queryBean.getGameproductid().trim());
				sql.append("' ");
			}
			if (queryBean.getGameorderid() != null
					&& !"".equals(queryBean.getGameorderid())) {
				sql.append(" and gameorderid = '");
				sql.append(queryBean.getGameorderid().trim());
				sql.append("' ");
			}
			if (queryBean.getPlatorderid() != null
					&& !"".equals(queryBean.getPlatorderid())) {
				sql.append(" and platorderid = '");
				sql.append(queryBean.getPlatorderid().trim());
				sql.append("' ");
			}
            sql.append(" and dt >= '");
            sql.append(Time.getDateStr(dateStart));
            sql.append("' and dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append("' ");
			sql.append(" order by logtime desc ");

			System.out.println(sql.toString());
			Entry<List<String>, List<Object[]>> ret = hadoopQueryManager
					.executeSQL(sql.toString());
			if (null == ret || null == ret.getKey() || ret.getKey().isEmpty()
					|| null == ret.getValue()) {
				context.addMessage(null, new FacesMessage("提示", "查询结果为空!"));
				return;
			}

			result = ret.getValue();
		} else {
			System.out
					.println("VISIT_QUEUE offer AuanyTransferorderFlow failed!");
			context.addMessage(null, new FacesMessage("提示",
					"当前系统正被访问，为保证服务器稳定，请稍后再试"));
			return;
		}
	}

	public class AuanyTransferorderFlow {
		public String dt;
		public String logtime;
		public String hostname;
		public String modulename;
		public String gameid;
		public String auanyserverid;
		public String logid;
		public String logtag;

		public int ordertype;
		public String fulluseridentity;
		public String serverid;
		public String roleid;
		public String gameorderid;
		public String createtime;
		public String gameproductid;
		public String ordercurrencytype;
		public int gameproductamount;
		public String platname;
		public String platorderid;
		public String platproductid;
		public String platcurrencytype;
		public int platproductamount;
		public int gamecurrencytype;
		public int gamecurrencynum;
		public int extracurrencynum;
		public int retcode;
		public int synsn;
		public int acksn;
		public String extra1;
		public String extra2;

		public String getDt() {
			return dt;
		}

		public void setDt(String dt) {
			this.dt = dt;
		}

		public String getLogtime() {
			return logtime;
		}

		public void setLogtime(String logtime) {
			this.logtime = logtime;
		}

		public String getHostname() {
			return hostname;
		}

		public void setHostname(String hostname) {
			this.hostname = hostname;
		}

		public String getModulename() {
			return modulename;
		}

		public void setModulename(String modulename) {
			this.modulename = modulename;
		}

		public String getGameid() {
			return gameid;
		}

		public void setGameid(String gameid) {
			this.gameid = gameid;
		}

		public String getAuanyserverid() {
			return auanyserverid;
		}

		public void setAuanyserverid(String auanyserverid) {
			this.auanyserverid = auanyserverid;
		}

		public String getLogid() {
			return logid;
		}

		public void setLogid(String logid) {
			this.logid = logid;
		}

		public String getLogtag() {
			return logtag;
		}

		public void setLogtag(String logtag) {
			this.logtag = logtag;
		}

		public int getOrdertype() {
			return ordertype;
		}

		public void setOrdertype(int ordertype) {
			this.ordertype = ordertype;
		}

		public String getFulluseridentity() {
			return fulluseridentity;
		}

		public void setFulluseridentity(String fulluseridentity) {
			this.fulluseridentity = fulluseridentity;
		}

		public String getServerid() {
			return serverid;
		}

		public void setServerid(String serverid) {
			this.serverid = serverid;
		}

		public String getRoleid() {
			return roleid;
		}

		public void setRoleid(String roleid) {
			this.roleid = roleid;
		}

		public String getGameorderid() {
			return gameorderid;
		}

		public void setGameorderid(String gameorderid) {
			this.gameorderid = gameorderid;
		}

		public String getCreatetime() {
			return createtime;
		}

		public void setCreatetime(String createtime) {
			this.createtime = createtime;
		}

		public String getGameproductid() {
			return gameproductid;
		}

		public void setGameproductid(String gameproductid) {
			this.gameproductid = gameproductid;
		}

		public String getOrdercurrencytype() {
			return ordercurrencytype;
		}

		public void setOrdercurrencytype(String ordercurrencytype) {
			this.ordercurrencytype = ordercurrencytype;
		}

		public int getGameproductamount() {
			return gameproductamount;
		}

		public void setGameproductamount(int gameproductamount) {
			this.gameproductamount = gameproductamount;
		}

		public String getPlatname() {
			return platname;
		}

		public void setPlatname(String platname) {
			this.platname = platname;
		}

		public String getPlatorderid() {
			return platorderid;
		}

		public void setPlatorderid(String platorderid) {
			this.platorderid = platorderid;
		}

		public String getPlatproductid() {
			return platproductid;
		}

		public void setPlatproductid(String platproductid) {
			this.platproductid = platproductid;
		}

		public String getPlatcurrencytype() {
			return platcurrencytype;
		}

		public void setPlatcurrencytype(String platcurrencytype) {
			this.platcurrencytype = platcurrencytype;
		}

		public int getPlatproductamount() {
			return platproductamount;
		}

		public void setPlatproductamount(int platproductamount) {
			this.platproductamount = platproductamount;
		}

		public int getGamecurrencytype() {
			return gamecurrencytype;
		}

		public void setGamecurrencytype(int gamecurrencytype) {
			this.gamecurrencytype = gamecurrencytype;
		}

		public int getGamecurrencynum() {
			return gamecurrencynum;
		}

		public void setGamecurrencynum(int gamecurrencynum) {
			this.gamecurrencynum = gamecurrencynum;
		}

		public int getExtracurrencynum() {
			return extracurrencynum;
		}

		public void setExtracurrencynum(int extracurrencynum) {
			this.extracurrencynum = extracurrencynum;
		}

		public int getRetcode() {
			return retcode;
		}

		public void setRetcode(int retcode) {
			this.retcode = retcode;
		}

		public int getSynsn() {
			return synsn;
		}

		public void setSynsn(int synsn) {
			this.synsn = synsn;
		}

		public int getAcksn() {
			return acksn;
		}

		public void setAcksn(int acksn) {
			this.acksn = acksn;
		}

		public String getExtra1() {
			return extra1;
		}

		public void setExtra1(String extra1) {
			this.extra1 = extra1;
		}

		public String getExtra2() {
			return extra2;
		}

		public void setExtra2(String extra2) {
			this.extra2 = extra2;
		}

	}

	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public AuanyTransferorderFlow getQueryBean() {
		return queryBean;
	}

	public void setQueryBean(AuanyTransferorderFlow queryBean) {
		this.queryBean = queryBean;
	}

	public List<Object[]> getResult() {
		return result;
	}

	public void setResult(List<Object[]> result) {
		this.result = result;
	}

	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}
}
